//
//  PKResultView.m
//  Learn
//
//  Created by apple on 2024/4/17.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "PKResultView.h"
#import "PkMatchModel.h"
#import "PKRequest.h"

@interface pkResultCell :UITableViewCell
/* <#描述#> */
@property (nonatomic, strong) UILabel    * wordLabel;
@property (nonatomic, strong) UILabel    * exLabel;

@property (nonatomic, strong) UIImageView    * stateIcon;
@property (nonatomic, strong) UIImageView    * stateIcon2;

@end

@implementation pkResultCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColor.clearColor;
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
- (void)createUI {

    [self.contentView addSubview:self.wordLabel];
    [self.contentView addSubview:self.exLabel];

    [self.contentView addSubview:self.stateIcon];
    [self.contentView addSubview:self.stateIcon2];
    
}
- (UIImageView *)stateIcon {
    if(!_stateIcon){
        _stateIcon = [[UIImageView alloc]initWithFrame:CGRectMake(10, 16, 18, 18)];
        _stateIcon.contentMode = UIViewContentModeScaleAspectFill;
        _stateIcon.image = [UIImage imageNamed:@"rightIcon"];//wrongIcon
    }
    return _stateIcon;
}
- (UIImageView *)stateIcon2 {
    if(!_stateIcon2){
        _stateIcon2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 16, 18, 18)];
        _stateIcon2.right = SCREEN_W - 28;
        _stateIcon2.contentMode = UIViewContentModeScaleAspectFill;
        _stateIcon2.image = [UIImage imageNamed:@"rightIcon"];//wrongIcon
    }
    return _stateIcon2;
}
- (UILabel *)wordLabel {
    if(!_wordLabel){
        _wordLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 + self.stateIcon.right, 0, 117, 20)];
        _wordLabel.centerY = self.stateIcon.centerY;
        _wordLabel.text = @"";
        _wordLabel.textColor = [UIColor blackColor];
        _wordLabel.font = PingFangMediumFont(14);
        _wordLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _wordLabel;
}
- (UILabel *)exLabel {
    if(!_exLabel){
        _exLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_W/2, 0, self.stateIcon2.left - SCREEN_W/2, 20)];
        _exLabel.centerY = self.stateIcon.centerY;
        _exLabel.text = @"";
        _exLabel.textColor = [UIColor blackColor];
        _exLabel.font = PingFangMediumFont(14);
        _exLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _exLabel;
}


- (void)updateItemCellModel:(id)itemModel left:(NSString *)left right:(NSString *)right{
    if ([itemModel isKindOfClass:PkWordModel.class]) {
        PkWordModel *model = (PkWordModel *)itemModel;
        self.exLabel.text = model.pretations;
        self.wordLabel.text = model.words;
        if (left.intValue == 0) {
            self.stateIcon.image = [UIImage imageNamed:@"wrongIcon"];
        }else{
            self.stateIcon.image = [UIImage imageNamed:@"rightIcon"];
        }
        if (right.intValue == 0) {
            self.stateIcon2.image = [UIImage imageNamed:@"wrongIcon"];
        }else{
            self.stateIcon2.image = [UIImage imageNamed:@"rightIcon"];
        }
    }
}

@end

@interface PKResultView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) PkMatchModel    * matchModel;

/* <#描述#> */
@property (nonatomic, strong) UIImageView    * resultImg;
@property (nonatomic, strong) UIImageView    * resultTitleImg;

@property (nonatomic, strong) UIView    * pkView;

@property (nonatomic, strong) UILabel    * pkLabel;
@property (nonatomic, strong) UILabel    * pkTimeLabel;

@property (nonatomic, strong) UIImageView    * leftWinIcon;
@property (nonatomic, strong) UIImageView    * rightWinIcon;

@property (nonatomic, strong) UIImageView    * leftUserImg;
@property (nonatomic, strong) UILabel    * leftUserName;
@property (nonatomic, strong) UIImageView    * rightUserImg;
@property (nonatomic, strong) UILabel    * rightUserName;

@property (nonatomic, strong) UIView    * pkResultBgView;

@property (nonatomic, strong) UIButton    * againBtn;
@property (nonatomic, strong) UITableView    * mTableView;

@property (nonatomic, strong) NSMutableArray    * myResultArray;
@property (nonatomic, strong) NSMutableArray    * otherResultArray;

@property (nonatomic, assign) BOOL            isWin;       //
@end
@implementation PKResultView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}
- (void)createUI {
    [self addSubview:self.resultImg];
    [self addSubview:self.resultTitleImg];
    [self addSubview:self.pkView];
    [self addSubview:self.pkResultBgView];

}
- (UIImageView *)resultImg {
    if(!_resultImg){
        _resultImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 66,66)];
        _resultImg.centerX = self.width/2;
        _resultImg.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _resultImg;
}
- (UIImageView *)resultTitleImg {
    if(!_resultTitleImg){
        _resultTitleImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 30 + self.resultImg.bottom, 152,23)];
        _resultTitleImg.centerX = self.width/2;
        _resultTitleImg.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _resultTitleImg;
}
- (UIView *)pkView {
    if(!_pkView){
        _pkView = [[UIView alloc]initWithFrame:CGRectMake(28, 30 + self.resultTitleImg.bottom, SCREEN_W - 56, 111 )];
        
        _pkView.backgroundColor = [UIColor whiteColor];
        [_pkView js_setRoundedCorners:UIRectCornerAllCorners radii:15];
        [_pkView addSubview:self.pkLabel];
        [_pkView addSubview:self.pkTimeLabel];

        [_pkView addSubview:self.leftUserImg];
        [_pkView addSubview:self.leftWinIcon];

        [_pkView addSubview:self.leftUserName];
        [_pkView addSubview:self.rightUserImg];
        [_pkView addSubview:self.rightWinIcon];

        [_pkView addSubview:self.rightUserName];
    }
    return _pkView;
}
- (UILabel *)pkLabel {
    if(!_pkLabel){
        _pkLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 21, 100, 33)];
        _pkLabel.centerX = self.pkView.width/2;
        _pkLabel.text = @"0-0";
        _pkLabel.textColor = COLOR_TOPIC_SELECT;
        _pkLabel.font = PingFangMediumFont(24);
        _pkLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _pkLabel;
}
- (UILabel *)pkTimeLabel {
    if(!_pkTimeLabel){
        _pkTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.pkLabel.bottom, 100, 33)];
        _pkTimeLabel.centerX = self.pkView.width/2;
        _pkTimeLabel.text = @"";
        _pkTimeLabel.textColor = COLOR_NOT_SELECT;
        _pkTimeLabel.font = PingFangMediumFont(12);
        _pkTimeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _pkTimeLabel;
}
- (UIImageView *)leftUserImg {
    if(!_leftUserImg){
        _leftUserImg = [[UIImageView alloc]initWithFrame:CGRectMake(47, 15, 44, 44)];
        _leftUserImg.contentMode = UIViewContentModeScaleAspectFill;
//        _leftUserImg.image = [UIImage imageNamed:@"1"];
        [_leftUserImg js_setRoundedCorners:UIRectCornerAllCorners radii:22];
    }
    return _leftUserImg;
}
- (UIImageView *)leftWinIcon {
    if(!_leftWinIcon){
        _leftWinIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 28, 28)];
        _leftWinIcon.center = CGPointMake(self.leftUserImg.right, self.leftUserImg.top);
        _leftWinIcon.contentMode = UIViewContentModeScaleAspectFill;
        _leftWinIcon.image = [UIImage imageNamed:@"pk_huangguan"];
        _leftWinIcon.hidden = YES;
    }
    return _leftWinIcon;
}
- (UILabel *)leftUserName {
    if(!_leftUserName){
        _leftUserName = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 + self.leftUserImg.bottom, 100, 22)];
        _leftUserName.centerX = self.leftUserImg.centerX;

//        _leftUserName.text = @"张小明";
        _leftUserName.textColor = [UIColor blackColor];
        _leftUserName.font = PingFangMediumFont(16);
        _leftUserName.textAlignment = NSTextAlignmentCenter;
    }
    return _leftUserName;
}
- (UIImageView *)rightUserImg {
    if(!_rightUserImg){
        _rightUserImg = [[UIImageView alloc]initWithFrame:CGRectMake(self.pkView.width - 91, 15, 44, 44)];
        _rightUserImg.contentMode = UIViewContentModeScaleAspectFill;
//        _rightUserImg.image = [UIImage imageNamed:@"1"];
        [_rightUserImg js_setRoundedCorners:UIRectCornerAllCorners radii:22];
    }
    return _rightUserImg;
}
- (UIImageView *)rightWinIcon {
    if(!_rightWinIcon){
        _rightWinIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 28, 28)];
        _rightWinIcon.center = CGPointMake(self.rightUserImg.right, self.rightUserImg.top);
        _rightWinIcon.contentMode = UIViewContentModeScaleAspectFill;
        _rightWinIcon.image = [UIImage imageNamed:@"pk_huangguan"];
        _rightWinIcon.hidden = YES;
    }
    return _rightWinIcon;
}
- (UILabel *)rightUserName {
    if(!_rightUserName){
        _rightUserName = [[UILabel alloc]initWithFrame:CGRectMake(0, 10+ self.rightUserImg.bottom, 100, 22)];
        _rightUserName.centerX = self.rightUserImg.centerX;
//        _rightUserName.text = @"张小明";
        _rightUserName.textColor = [UIColor blackColor];
        _rightUserName.font = PingFangMediumFont(16);
        _rightUserName.textAlignment = NSTextAlignmentCenter;
    }
    return _rightUserName;
}
- (UIView *)pkResultBgView {
    if(!_pkResultBgView){
        _pkResultBgView = [[UIView alloc]initWithFrame:CGRectMake(10, 10 + self.pkView.bottom, SCREEN_W - 20, SCREEN_W - 15 )];
        
        _pkResultBgView.backgroundColor = [UIColor whiteColor];
        [_pkResultBgView js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_pkResultBgView addSubview:self.againBtn];
        [_pkResultBgView addSubview:self.mTableView];

       
    }
    return _pkResultBgView;
}

- (UIButton *)againBtn {
    if(!_againBtn){
        _againBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _againBtn.frame = CGRectMake(28, self.pkResultBgView.height -75, self.pkResultBgView.width - 56, 45);
        [_againBtn setTitle:@"再来一局" forState:UIControlStateNormal];
        
        [_againBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _againBtn.titleLabel.font  = PingFangMediumFont(18);
        _againBtn.az_colors = @[(__bridge id)rgbHex(0x02A1F9, 1).CGColor,(__bridge id)rgbHex(0x29F19C,1).CGColor];
        _againBtn.az_startPoint = CGPointMake(0, 0.5);
        _againBtn.az_endPoint = CGPointMake(1, 0.5);
        [_againBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_againBtn addTarget:self action:@selector(againBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _againBtn;
}

- (UITableView *)mTableView {
    if(!_mTableView){
        _mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.pkResultBgView.width, self.againBtn.top) style:UITableViewStylePlain];
        _mTableView.backgroundColor = UIColor.clearColor;
        _mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mTableView.delegate = self;
        _mTableView.dataSource = self;
        [_mTableView registerClass:pkResultCell.class forCellReuseIdentifier:@"pkResultCell"];
    }
    return _mTableView;
}

#pragma mark ==================== tabledelegate                ======================
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.matchModel.wordsList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 51;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    pkResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pkResultCell"];
    [cell updateItemCellModel:self.matchModel.wordsList[indexPath.row]left:self.myResultArray[indexPath.row] right:self.otherResultArray[indexPath.row]];
    return cell;
}



#pragma mark   刷新UI
- (void)updateUIWithModel:(id)model {
    if ([model isKindOfClass:PkMatchModel.class]) {
        PkMatchModel *matchModel = (PkMatchModel *)model;
        self.matchModel = matchModel;
        [self.mTableView reloadData];
        [self.leftUserImg sd_setImageWithURL:[NSURL URLWithString:lApp.useModel.avatar]placeholderImage:[UIImage imageNamed:@"pk_userHeader"]];
        self.leftUserName.text = lApp.useModel.nickname;
        [self.rightUserImg sd_setImageWithURL:[NSURL URLWithString:matchModel.otherUser.avatar]placeholderImage:[UIImage imageNamed:@"pk_userHeader"]];
        self.rightUserName.text = matchModel.otherUser.nickname;
    }
}


- (void)updateUIWithMytime:(NSInteger)myTime otherTime:(NSInteger)otherTime myArray:(NSArray *)myArray otherArray:(NSArray *)otherArray {
    self.myResultArray = myArray.mutableCopy;
    self.otherResultArray = otherArray.mutableCopy;
    
    int mySum = [[myArray valueForKeyPath:@"@sum.intValue"] intValue];
    int otherSum = [[otherArray valueForKeyPath:@"@sum.intValue"] intValue];
    if (mySum > otherSum) {
        self.isWin = YES;
    }else if(mySum < otherSum){
        self.isWin = NO;
    }else{
        if (myTime <= otherTime) {
            self.isWin = YES;
        }else {
            self.isWin = NO;
        }
    }
    [self requestSave];
    self.pkLabel.text = [NSString stringWithFormat:@"%d-%d",mySum,otherSum];
    self.resultImg.image = self.isWin ? [UIImage imageNamed:@"pk_win"]:[UIImage imageNamed:@"pk_lose"];
    self.resultTitleImg.image = self.isWin ? [UIImage imageNamed:@"pk_win_t"]:[UIImage imageNamed:@"pk_lose_t"];
    
    self.pkTimeLabel.text = [NSString stringWithFormat:@"%.1fs : %.1fs",myTime/10.0,otherTime/10.0];
    
    self.leftWinIcon.hidden = !self.isWin;
    self.rightWinIcon.hidden = self.isWin;
    
    if (self.matchModel.wordsList.count == myArray.count) {
        [self.mTableView reloadData];
    }
}




- (void)againBtnClicked:(UIButton *)sender {
    if (self.againBlock) {
        self.againBlock();
    }
}

- (void)requestSave {
    [PKRequest request_postSavePkResultType:self.isWin ? @"1":@"0" reslut:^(BOOL isRequest) {
            
    }];
}
@end
